package com.building.hcrm.ui.customer;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RadioButton;

import com.building.hcrm.R;
import com.building.hcrm.R.id;
import com.building.hcrm.R.string;
import com.building.hcrm.base.BaseActivity;
import com.building.hcrm.base.BaseCustomerActivity;
import com.building.hcrm.common.ComConst;
import com.building.hcrm.model.CustomerDetail;

public class CustomerInvestment extends BaseCustomerActivity {

	private EditText etHouseNum;
	private EditText etHouseArea;
	private RadioButton radioHouse1;
	private RadioButton radioHouse2;
	private EditText etShopNum;
	private EditText etShopPrice;
	private RadioButton radioShop1;
	private RadioButton radioShop2;
	private ImageView ivStock;
	private ImageView ivFund;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.partial_customer_investment);
		initControls();
		initDatas();
		setOnAllListeners();
	}

	private void initControls() {
		etHouseNum = (EditText) findViewById(id.et_house_num);
		etHouseArea = (EditText) findViewById(id.et_house_area);
		radioHouse1 = (RadioButton) findViewById(id.radio_house1);
		radioHouse2 = (RadioButton) findViewById(id.radio_house2);
		etShopNum = (EditText) findViewById(id.et_shop_num);
		etShopPrice = (EditText) findViewById(id.et_shop_price);
		radioShop1 = (RadioButton) findViewById(id.radio_shop1);
		radioShop2 = (RadioButton) findViewById(id.radio_shop2);
		ivStock = (ImageView) findViewById(id.iv_stock);
		ivFund = (ImageView) findViewById(id.iv_fund);
		imgbtnHome = (ImageButton) findViewById(id.imgbtn_home);
		imgbtnHome.setVisibility(View.VISIBLE);
	}

	private void initDatas() {
		setTitle(getResources().getString(string.customer_title_investment));
		mCustomerDetail = (CustomerDetail) getIntent().getSerializableExtra(
				ComConst.CUSTOMER_DATAS);
		if (mCustomerDetail == null) {
			mCustomerDetail = new CustomerDetail();
		} else {
			displayDatas();
		}
	}

	private void displayDatas() {
		if (mCustomerDetail.CustomerInvestHouseTao != null
				&& !("").equals(mCustomerDetail.CustomerInvestHouseTao)) {
			etHouseNum.setText(mCustomerDetail.CustomerInvestHouseTao);
		}
		if (mCustomerDetail.CustomerInvestHousePing != null
				&& !("").equals(mCustomerDetail.CustomerInvestHousePing)) {
			etHouseArea.setText(mCustomerDetail.CustomerInvestHousePing);
		}
		if (mCustomerDetail.CustomerInvestHouseType != null
				&& !("").equals(mCustomerDetail.CustomerInvestHouseType)) {
			if (mCustomerDetail.CustomerInvestHouseType.equals("1")) {
				radioHouse1.setChecked(true);
			} else {
				radioHouse2.setChecked(true);
			}
		}
		if (mCustomerDetail.CustomerInvestShopTao != null
				&& !("").equals(mCustomerDetail.CustomerInvestShopTao)) {
			etShopNum.setText(mCustomerDetail.CustomerInvestShopTao);
		}
		if (mCustomerDetail.CustomerInvestShopTotal != null
				&& !("").equals(mCustomerDetail.CustomerInvestShopTotal)) {
			etShopPrice.setText(mCustomerDetail.CustomerInvestShopTotal);
		}
		if (mCustomerDetail.CustomerInvestShopType != null
				&& !("").equals(mCustomerDetail.CustomerInvestShopType)) {
			if (mCustomerDetail.CustomerInvestShopType.equals("1")) {
				radioShop1.setChecked(true);
			} else {
				radioShop1.setChecked(true);
			}
		}
		if (mCustomerDetail.CustomerInvestStock != null
				&& !("").equals(mCustomerDetail.CustomerInvestStock)) {
			if (mCustomerDetail.CustomerInvestStock.equals("1")) {
				ivStock.setImageDrawable(getResources().getDrawable(
						R.drawable.checked));
			}
		}
		if (mCustomerDetail.CustomerInvestFund != null
				&& !("").equals(mCustomerDetail.CustomerInvestFund)) {
			if (mCustomerDetail.CustomerInvestFund.equals("1")) {
				ivFund.setImageDrawable(getResources().getDrawable(
						R.drawable.checked));
			}
		}
	}

	private void setOnAllListeners() {
		setImageViewListener(ivStock);
		setImageViewListener(ivFund);

		imgbtnHome.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				getDatas();
				Intent intent = new Intent();
				intent.putExtra(ComConst.CUSTOMER_DATAS, mCustomerDetail);
				setResult(ComConst.CUSTOMER_INVESTMENT, intent);
				imgbtnHome.setVisibility(View.INVISIBLE);
				finish();
				slideReturnActivity();
			}
		});
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			imgbtnHome.performClick();
		}
		return false;
	}

	/**
	 * 设置ImageView的点击事件
	 * 
	 * @param iv
	 */
	private void setImageViewListener(final ImageView iv) {
		iv.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (iv.getDrawable()
						.getConstantState()
						.equals(getResources()
								.getDrawable(R.drawable.unchecked)
								.getConstantState())) {
					// 设置图片
					iv.setImageDrawable(getResources().getDrawable(
							R.drawable.checked));
				} else {
					// 设置图片
					iv.setImageDrawable(getResources().getDrawable(
							R.drawable.unchecked));
				}

			}
		});
	}

	/**
	 * 获取页面的数据
	 */
	private void getDatas() {
		mCustomerDetail.CustomerInvestHouseTao = etHouseNum.getText()
				.toString();
		mCustomerDetail.CustomerInvestHousePing = etHouseArea.getText()
				.toString();
		if (!mCustomerDetail.CustomerInvestHouseTao.equals("")
				&& !mCustomerDetail.CustomerInvestHousePing.equals("")) {
			if (radioHouse1.isChecked()) {
				mCustomerDetail.CustomerInvestHouseType = "1";
			} else if (radioHouse2.isChecked()) {
				mCustomerDetail.CustomerInvestHouseType = "2";
			}
		}

		mCustomerDetail.CustomerInvestShopTao = etShopNum.getText().toString();
		mCustomerDetail.CustomerInvestShopTotal = etShopPrice.getText()
				.toString();
		if (!mCustomerDetail.CustomerInvestShopTao.equals("")
				&& !mCustomerDetail.CustomerInvestShopTotal.equals("")) {
			// 商铺套数和总价都填写才能保存按揭或者一次性
			if (radioShop1.isChecked()) {
				mCustomerDetail.CustomerInvestShopType = "1";
			} else if (radioShop2.isChecked()) {
				mCustomerDetail.CustomerInvestShopType = "2";
			}
		}

		if (ivStock
				.getDrawable()
				.getConstantState()
				.equals(getResources().getDrawable(R.drawable.unchecked)
						.getConstantState())) {
			mCustomerDetail.CustomerInvestStock = "0";
		} else {
			mCustomerDetail.CustomerInvestStock = "1";
		}

		if (ivFund
				.getDrawable()
				.getConstantState()
				.equals(getResources().getDrawable(R.drawable.unchecked)
						.getConstantState())) {
			mCustomerDetail.CustomerInvestFund = "0";
		} else {
			mCustomerDetail.CustomerInvestFund = "1";
		}
	}

}
